package com.qf.zyh.web.controller;

import com.qf.zyh.entity.EdocEntry;
import com.qf.zyh.services.EntryServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/entry")
public class EntryController {
    @Autowired
    private EntryServices es;
    @RequestMapping("/index")
    public String index(Model m){
        Map<String, Object> all = es.getAll();
        m.addAttribute("list",all);
        return "index";
    }
    @RequestMapping("/sel")
    public String select(Model m,String name){
        System.out.println(name);
        Map<String, Object> byName = es.getByName(name);
        System.out.println(byName);
        m.addAttribute("list",byName);
        return "index";
    }
    @RequestMapping("/del")
    public String select(int id, HttpSession session){
        int i =es.delById(id);
        System.out.println(i+"删除的");
        if (i==1) {
            session.setAttribute("msg","删除成功");
        }else {
            session.setAttribute("msg","删除失败");
        }
        return "redirect:/entry/index";
    }
    @RequestMapping("/upden")
    public String select(Model m,int id){
        EdocEntry byId = es.getById(id);
        m.addAttribute("edoc",byId);
        return "upda";
    }
    @RequestMapping("/addEntry")
    public String addEntry(EdocEntry ee){
        int i = es.addEntry(ee);
        System.out.println(i+"添加");
        return "redirect:/entry/index";
    }
    @RequestMapping("/updaEntry")
    public String updaEntry(EdocEntry ee){
        int i = es.updaEntry(ee);
        System.out.println(i+"修改");
        return "redirect:/entry/index";
    }
}
